package tut.net.json.javaxJson;

import java.io.FileInputStream;

import javax.json.Json;
import javax.json.stream.JsonParser;
import javax.json.stream.JsonParser.Event;

public class ReportParser  {
	private static final String TAG_ADSPOT_ID = "adspot_id";
	private static final String TAG_IMP = "imp";
	private static final String TAG_CLICK = "click";
	private static final String TAG_PAYMENT = "payment";

	private int total;
	int format;// xml, json

	public ReportParser() {
	}

	/*
	 * public int paser(String jsonStr) throws Exception {
	 * 
	 * 
	 * JsonParserFactory factory = Json.createParserFactory(null); JsonParser
	 * parser = factory.createParser(new FileInputStream(""));
	 * 
	 * parser.
	 * 
	 * while (parser.hasNext()) { Event event = parser.next();
	 * 
	 * switch (event) { case KEY_NAME: { System.out.print(parser.getString() +
	 * "="); break; } case VALUE_STRING: {
	 * System.out.println(parser.getString()); break; } } }
	 * 
	 * JsonObject myjson = new JsonObject(jsonStr); JSONArray the_json_array =
	 * myjson.getJSONArray("profiles");
	 * 
	 * 
	 * String FILE_NAME =
	 * "C:\\\\Users\\\\dientn\\\\Desktop\\\\liveaid\\\\test data\\\\employee.txt";
	 * InputStream fis = new FileInputStream(FILE_NAME); final JsonParser parser
	 * = Json.createParser(fis); //final JsonElement jsonElement =
	 * parser.parse(new FileReader("/path/to/myfile")); Json final JsonObject
	 * jsonObject = jsonElement.getAsJsonObject();
	 * 
	 * for (final Entry<String, JsonElement> entry : jsonObject.entrySet()) {
	 * final String key = entry.getKey(); final JsonElement value =
	 * entry.getValue(); .... }
	 * 
	 * 
	 * if (jsonStr != null) { try { JSONArray jsonArray = new
	 * JSONArray(jsonString); JSONObject jsonObj = new JSONObject(jsonStr);
	 * 
	 * // Getting JSON Array node contacts = jsonObj.getJSONArray(TAG_CONTACTS);
	 * 
	 * // looping through All Contacts for (int i = 0; i < contacts.length();
	 * i++) { JSONObject c = contacts.getJSONObject(i);
	 * 
	 * String id = c.getString(TAG_ID); String name = c.getString(TAG_NAME);
	 * String email = c.getString(TAG_EMAIL); String address =
	 * c.getString(TAG_ADDRESS); String gender = c.getString(TAG_GENDER);
	 * 
	 * // Phone node is JSON Object JSONObject phone =
	 * c.getJSONObject(TAG_PHONE); String mobile =
	 * phone.getString(TAG_PHONE_MOBILE); String home =
	 * phone.getString(TAG_PHONE_HOME); String office =
	 * phone.getString(TAG_PHONE_OFFICE);
	 * 
	 * // tmp hashmap for single contact HashMap<String, String> contact = new
	 * HashMap<String, String>();
	 * 
	 * // adding each child node to HashMap key => value contact.put(TAG_ID,
	 * id); contact.put(TAG_NAME, name); contact.put(TAG_EMAIL, email);
	 * contact.put(TAG_PHONE_MOBILE, mobile);
	 * 
	 * // adding contact to contact list contactList.add(contact); } } catch
	 * (JSONException e) { e.printStackTrace(); } } else { } }
	 */
	/*
	 * public static void main(String args[]) throws Exception { String str =
	 * "C:\\\\Users\\\\dientn\\\\Desktop\\\\liveaid\\\\test data\\\\employee.txt";
	 * InputStream fis = new FileInputStream(str);
	 * 
	 * // create JsonReader object JsonReader jsonReader =
	 * Json.createReader(fis);
	 * 
	 * 
	 * // We can create JsonReader from Factory also JsonReaderFactory factory
	 * // = Json.createReaderFactory(null); jsonReader = //
	 * factory.createReader(fis);
	 * 
	 * 
	 * // get JsonObject from JsonReader JsonObject jsonObject =
	 * jsonReader.readObject();
	 * 
	 * // we can close IO resource and JsonReader now jsonReader.close();
	 * fis.close();
	 * 
	 * JsonArray jsonArr = jsonObject.getJsonArray("phoneNumbers");
	 * 
	 * int size = jsonArr.size(); ArrayList<JsonObject> arrays = new
	 * ArrayList<JsonObject>(); for (int i = 0; i < size; i++) { JsonObject
	 * another_json_object = jsonArr.getJsonObject(i);
	 * arrays.add(another_json_object); }
	 * 
	 * for (int i = 0; i < arrays.size(); i++) { JsonObject o = arrays.get(i);
	 * System.out.println(o.toString()); }
	 * 
	 * }
	 */
	public static void main(String args[]) throws Exception {
		try (FileInputStream json = new FileInputStream("C:\\Users\\dientn\\Desktop\\liveaid\\test data\\test.txt")) {
			JsonParser jr = Json.createParser(json);
			Event event = null;
			String name = null;
			// Advance to "address" key
			while (jr.hasNext()) {
				event = jr.next();
				if (event == Event.KEY_NAME ){
					name = jr.getString();
					//if(name.equals("result")) {
					if(name.equals("total")) {
						event = jr.next();
						getResult(jr, event);
						break;
					}
				}
			}

			// Output contents of "address" object
			while (event != Event.END_OBJECT) {
				switch (event) {
				case KEY_NAME: {
					System.out.print(jr.getString());
					System.out.print(" = ");
					break;
				}
				case VALUE_FALSE: {
					System.out.println(false);
					break;
				}
				case VALUE_NULL: {
					System.out.println("null");
					break;
				}
				case VALUE_NUMBER: {
					if (jr.isIntegralNumber()) {
						System.out.println(jr.getInt());
					} else {
						System.out.println(jr.getBigDecimal());
					}
					break;
				}
				case VALUE_STRING: {
					System.out.println(jr.getString());
					break;
				}
				case VALUE_TRUE: {
					System.out.println(true);
					break;
				}
				default: {
				}
				}
				event = jr.next();
			}
		}
	}
	
	
	//0=>200, 1=204, 2=>other
	private static void getResult(JsonParser jr, Event event){
		while (event != Event.END_OBJECT) {
			switch (event) {
			}
			event = jr.next();
		}
	}
	
	public int getTotal() {
		return this.total;
	}
}
